package news.newsmain.controller;

import com.alibaba.fastjson.JSON;
import news.newsmain.entity.NewsPublish;
import news.newsmain.service.IMainNewsService;
import news.newsmain.service.MainNewsServiceImpl;
import news.newstopic.entity.NewsTopic;
import news.util.BaseHttpServlet;
import news.util.FileUtils;
import news.util.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author Gong
 * @Description
 * @dATE 2022/2/27 15:52
 **/
@WebServlet("/MainNews")
public class MainNewsController extends BaseHttpServlet {
    private IMainNewsService iMainNewsService = new MainNewsServiceImpl();
    /**
     * @Author GongXL
     * @Description  查询新闻方法并跳转
     * @Date 11:53 2022/2/28
     * @Param [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return void
     **/
    public void mainnewslist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //1.查询数据库新闻集合
        //查询数据库jdbc,并封装实体类
        List<NewsPublish> newslist = iMainNewsService.getNewsPublisList();//从数据库查询的
        req.setAttribute("newslist",newslist);
        //2.设置数据，返回页面，转发方式跳转
        req.getRequestDispatcher(req.getContextPath()+"/index.jsp").forward(req,resp);
    }
    /**
     * @Author GongXL
     * @Description  分页查询
     * @Date 12:00 2022/2/28
     * @Param [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return void
     **/
    public void mainnewsPageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.分页查询数据库新闻集合
        PageBean<NewsPublish> pageBean =  new PageBean();
        //TODO 设置当前页码
        //TODO 总记录数需要设置
        pageBean.setTotalCount(100);


        PageBean pageInfo = iMainNewsService.getNewsPublisPageList(pageBean);
        //分页信息+分页的集合数据
        req.setAttribute("pageInfo",pageInfo);
        //2.设置数据，返回页面，转发方式跳转
        req.getRequestDispatcher(req.getContextPath()+"/news/news_main/mainnews-list.jsp").forward(req,resp);
    }
    /**
     * @Author GongXL
     * @Description  ajax实现新闻分页
     * @Date 16:46 2022/3/3
     * @Param [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return void
     **/
    public void mainnewsPageListAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        //1.分页查询数据库新闻集合
        PageBean<NewsPublish> pageBean =  new PageBean();
        //设置当前页码
        Integer currPageNo = req.getParameter("currPageNo") !=null ? Integer.parseInt(req.getParameter("currPageNo")) : 1;
        //总记录数需要设置
        int totalCount =  iMainNewsService.getNewsPublisCount(null);
        pageBean.setCurrPageNo(currPageNo);
        pageBean.setTotalCount(totalCount);

        PageBean pageInfo = iMainNewsService.getNewsPublisPageList(pageBean);
       //pageInfo ajax的数据
        PrintWriter writer = resp.getWriter();
//        JSON
        String resJson = JSON.toJSONString(pageInfo);
        writer.write(resJson);
        writer.flush();
        writer.close();
    }
    /**
     * @Author GongXL
     * @Description  添加新闻信息
     * @Date 14:43 2022/2/28
     * @Param [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @return void
     **/
    public void addMainNews(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //TODO 存储各种数据信息
        String topid = req.getParameter("topid");
        //TODO 主题和新闻的关系信息
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        NewsPublish newsPublish = new NewsPublish();
        newsPublish.setTitle(title);
        newsPublish.setContent(content);
        //TODO 存储新闻

        //TODO 文件上传，得到的路径，将路径存数据
        List<String> filePaths = FileUtils.filesUpload(req, resp);
        System.out.println(Arrays.toString(filePaths.toArray()));
        //TODO 存储文件路径

        //TODO 跳转成功列表分页页面
        mainnewsPageList(req,resp);
    }



}
